﻿using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Zinnia.Lite.Untilty
{
    /// <summary>
    /// DES秘钥解析帮助类
    /// </summary>
    public class DesKeyResolveHelper
    {
        /// <summary>
        /// 加密秘钥转成解密秘钥(单向转)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string EncryptKeyToDecryptKey(string key)
        {
            return EncryptKeyToDecryptKeyBytes(key).Aggregate(string.Empty, (current, b) => current + (char)b);
        }

        /// <summary>
        /// 加密秘钥转成解密秘钥byte[]数组(单向转)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static byte[] EncryptKeyToDecryptKeyBytes(string key)
        {
            using (var md5CryptoServiceProvider = new MD5CryptoServiceProvider())
            {
                var data = md5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(key), 0, key.Length);
                var builder = new StringBuilder();
                foreach (var t in data)
                {
                    builder.Append(t.ToString("x2"));
                }
                return Encoding.ASCII.GetBytes(builder.ToString().Substring(0, 8));
            }
        }
    }
}
